package com.lyb.spzx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyb.spzx.manager.mapper.SysRoleMapper;
import com.lyb.spzx.manager.mapper.SysRoleUserMapper;
import com.lyb.spzx.manager.service.SysRoleService;
import com.lyb.spzx.model.dto.system.system.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Override
    public PageInfo<SysRole> findByPage(String keyword, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysRole> roleList = sysRoleMapper.findByPage(keyword);
        PageInfo<SysRole> pageInfo = new PageInfo<>(roleList);
        return pageInfo;
    }

    @Override
    public void saveSysRole(SysRole sysRole) {
        //生成创建时间和更新时间软删除标志
        sysRole.setCreateTime(new Date());
        sysRole.setUpdateTime(new Date());
        sysRole.setIsDeleted(0);
        sysRoleMapper.saveSysRole(sysRole);
    }

    @Override
    public void updateSysRole(SysRole sysRole) {
        sysRole.setUpdateTime(new Date());
        sysRoleMapper.updateSysRole(sysRole);
    }

    @Override
    public void deleteById(Integer id) {
        sysRoleMapper.deleteById(id);
    }

    @Override
    public Map<String, Object> findAll(Long id) {
        System.out.println("id = " + id);
        List<SysRole> roleList = sysRoleMapper.findAll();
        List<Long> userRoleById = sysRoleUserMapper.findUserRoleById(id);
        System.out.println("userRoleById = " + userRoleById);

        Map<String, Object> map = new HashMap<>();
        map.put("allRoles",roleList);
        map.put("userRoleIds",userRoleById);
        return map;
    }
}
